#include <stdio.h>
#include <math.h>
#include "../app.h"
#include "../font/font.h"
#include "dvc.h"

typedef struct infos
{
    const char *const name;
    lv_align_t align;
    lv_coord_t pct_x;
    lv_coord_t pct_y;
    lv_color_t bg_color;
    lv_color_t color;
    const char *const unit;
} coulomb_counte_info1s_t;

static const coulomb_counte_info1s_t infos_map[] = {
    {"累计充电", LV_ALIGN_TOP_LEFT, 0, 0, LV_COLOR_MAKE(104, 152, 252), LV_COLOR_MAKE(255, 255, 255), "WH"},
    {"今日充电", LV_ALIGN_TOP_RIGHT, 0, 0, LV_COLOR_MAKE(104, 152, 252), LV_COLOR_MAKE(255, 255, 255), "WH"},
    {"累计充容", LV_ALIGN_BOTTOM_LEFT, 0, 0, LV_COLOR_MAKE(0, 162, 211), LV_COLOR_MAKE(255, 255, 255), "AH"},
    {"今日充容", LV_ALIGN_BOTTOM_RIGHT, 0, 0, LV_COLOR_MAKE(0, 162, 211), LV_COLOR_MAKE(255, 255, 255), "AH"},
};

#define coulomb_counte_info1S_SIZE (sizeof(infos_map) / sizeof(infos_map[0]))

extern dvc_t gloab_dvc;
static lv_obj_t *numer_objs[coulomb_counte_info1S_SIZE] = {0};

static void timer_task_cb(struct _lv_timer_t *timer)
{
    if (!gloab_dvc)
        return;

    float *datas[coulomb_counte_info1S_SIZE] = {
        &gloab_dvc->coulomb_counter.sum.mW,
        &gloab_dvc->coulomb_counter.chg.mW,
        &gloab_dvc->coulomb_counter.sum.mA,
        &gloab_dvc->coulomb_counter.chg.mA,
    };

    for (uint8_t i = 0; i < coulomb_counte_info1S_SIZE; i++)
    {
        if (!numer_objs[i])
            continue;
        int32_t value = 0;

        static const char *const c_units[] = {"GAh", "MAh", "kAh", "Ah", "mAh", "uAh"};
        static const char *const p_units[] = {"GWh", "MWh", "KWh", "Wh", "mWh", "uWh"};
        static const uint8_t max_units = sizeof(c_units) / sizeof(c_units[0]);
        uint8_t idx = max_units - 2;

        float tmp = fabs(*datas[i]);
        tmp /= 3.6;

        if (tmp < 1.0f)
        {
            tmp *= 1000.0f;
            idx++;
        }

        while (tmp > 1000.0f)
        {
            if (idx == 0)
                break;
            tmp /= 1000.0f;
            idx--;
        }

        value = tmp * 100;
        if (*datas[i] >= 0.000000f)
            lv_label_set_text_fmt(numer_objs[i], "%lu.%02lu %s", value / 100, value % 100, i > 1 ? c_units[idx] : p_units[idx]);
        else
            lv_label_set_text_fmt(numer_objs[i], "-%lu.%02lu %s", value / 100, value % 100, i > 1 ? c_units[idx] : p_units[idx]);
        lv_obj_align(numer_objs[i], LV_ALIGN_BOTTOM_MID, 0, 10);
    }
}
lv_timer_t *app_coulomb_counte_info1_create(lv_obj_t *parent, uint8_t id)
{
    if (!parent)
        return NULL;

    lv_obj_t *obj = lv_tileview_add_tile(parent, id, 0, LV_DIR_HOR);
    lv_obj_set_style_pad_all(obj, 10, LV_PART_MAIN);
    lv_obj_set_style_text_font(obj, base_font16, LV_PART_MAIN);
    lv_obj_set_style_bg_color(obj, lv_color_make(0, 0, 0), LV_PART_MAIN);

    const uint8_t con_size = 100 / (coulomb_counte_info1S_SIZE / 2) - coulomb_counte_info1S_SIZE / 2 - 1;
    for (uint8_t i = 0; i < coulomb_counte_info1S_SIZE; i++)
    {
        /* main */
        lv_obj_t *tmp = lv_obj_create(obj);
        lv_obj_set_scroll_dir(tmp, LV_DIR_NONE); /* 不允许滑动 - 没用触摸 - 禁用滑动条 */
        lv_obj_set_size(tmp, LV_PCT(con_size), LV_PCT(con_size));
        lv_obj_align(tmp, infos_map[i].align, infos_map[i].pct_x, infos_map[i].pct_x);
        lv_obj_set_style_border_width(tmp, 0, LV_PART_MAIN);
        lv_obj_set_style_bg_color(tmp, infos_map[i].bg_color, LV_PART_MAIN);
        lv_obj_set_style_text_color(tmp, infos_map[i].color, LV_PART_MAIN);
        lv_obj_set_style_bg_opa(tmp, LV_OPA_50, LV_PART_MAIN);
        /* title */
        lv_obj_t *title = lv_label_create(tmp);
        lv_label_set_text_static(title, infos_map[i].name);
        lv_obj_align(title, LV_ALIGN_TOP_LEFT, -8, -10);
        /* Content */
        lv_obj_t *number = lv_label_create(tmp);
        lv_label_set_text_fmt(number, "00.00 %s", infos_map[i].unit);
        lv_obj_align(number, LV_ALIGN_BOTTOM_MID, 0, 10);
        numer_objs[i] = number;
    }
    lv_timer_t *timer = lv_timer_create(timer_task_cb, 1000, obj);
    lv_timer_pause(timer);
    return timer;
}

app_s coulomb_counte_info1_page = {
    .name = "coulomb_counte_info1",
    .create = app_coulomb_counte_info1_create,
    .delete = NULL,
    .update = NULL,
    .user_data = NULL,
};
